//
//  SYBreakPointUploading.m
//  SanYiTeaching
//
//  Created by 郑浩然 on 2019/10/8.
//  Copyright © 2019年 SanYi. All rights reserved.
//

#import "SYBreakPointUploading.h"
//#import "TXUGCPublishOptCenter.h"
//#import "TXUGCPublish.h"
#import "SYProgressView.h"


@interface SYBreakPointUploading ()
/*
<TXVideoPublishListener>
@property (strong, nonatomic) TXUGCPublish *videoPublish;//腾讯视频上传
@property (strong, nonatomic) SYProgressView *proView;
*/

@end


@implementation SYBreakPointUploading

+ (SYBreakPointUploading *)shared {
    static SYBreakPointUploading *_shared = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _shared = [[SYBreakPointUploading alloc] init];
    });
    return _shared;
}

//- (instancetype)init {
//    self = [super init];
//    if (self) {
//        if (_videoPublish == nil) {
//            NSString *userid = GetDataFromNSUserDefaults(@"independence_ios");
//            _videoPublish = [[TXUGCPublish alloc] initWithUserID:userid];
//            _videoPublish.delegate = self;
//        }
//
//
//    }
//    return self;
//}
//视频上传
- (void)updateSignature:(NSString *)signature
              coverPath:(NSString *)coverPath
              videoPath:(NSString *)videoPath
               fileName:(NSString *)fileName
           enableResume:(BOOL)enableResume
                    obj:(id)obj
{
    
    
//    [SYRequestAPI bb_TutorServiceCloudPlaySignToken:Token success:^(id  _Nonnull dataList) {
//        NSString *sign = [dataList nilObjectForKey:@"cloudPlaySign"];
//        TXPublishParam *publishParam = [[TXPublishParam alloc] init];
//        publishParam.signature = sign; //@"OgxRfSp3M3g2dd5kOc0QcPdqiCpzZWNyZXRJZD1BS0lEdmw3S0xDYU1la3kxZ0RIeUhQTTlrQU9UTjVkMzRKQXAmY3VycmVudFRpbWVTdGFtcD0xNTcwNTMzOTIzJmV4cGlyZVRpbWU9MTYwMjI0Mjg3MyZyYW5kb209MTIzNDU2";//signature;
//        publishParam.coverPath = coverPath;
//        publishParam.videoPath = videoPath;
//        publishParam.fileName = fileName;
//        [self->_videoPublish publishVideo:publishParam];
//
//
//        self->_proView = [[NSBundle mainBundle] loadNibNamed:@"SYProgressView" owner:self options:nil].firstObject;
//        [self->_proView.progressView setProgress:0.0f animated:NO];
//        UIWindow *window = [UIApplication sharedApplication].windows.firstObject;
//        [window addSubview:self->_proView];
//
//        [UIView animateWithDuration:0.25 animations:^{
//            self.proView.frame = CGRectMake(0, 0, APP_WIDTH, APP_HEIGHT);
//
//        }];
//
//
//    } fail:^(NSError *error) {
//
//
//
//    }];
    
    
    
}

#pragma mark - TXVideoPublishListener
- (void)onPublishProgress:(NSInteger)uploadBytes totalBytes:(NSInteger)totalBytes {
    self.progress = (float)uploadBytes/totalBytes;
//    if (self.block) {
//        self.block((float)uploadBytes/totalBytes);
//    }
    NSLog(@"onPublishProgress [%ld/%ld]", uploadBytes, totalBytes);
//    self.proView.progressView.progress =  (float)uploadBytes/totalBytes;
}

//- (void)onPublishComplete:(TXPublishResult*)result {
//    NSString *string = [NSString stringWithFormat:@"上传完成，错误码[%d]，信息[%@]", result.retCode, result.retCode == 0? result.videoURL: result.descMsg];
//    if (result.retCode == 0) {
//        _proView.titLab.text = @"上传成功";
//    } else {
//        _proView.titLab.text = @"上传失败";
//    }
//
//    NSLog(@"onPublishComplete [%d/%@]--%@", result.retCode, result.retCode == 0? result.videoURL: result.descMsg,string);
//
//    [NSObject executeRunloop:^{
//        [self.proView removeFromSuperview];
//        if (self.pathBlock) {
//            self.pathBlock(result.videoURL);
//        }
//    } afterDelay:1];
//
//
//
//
//}

























//把视频存储到本地
- (NSString *)writeToCacheVideo:(NSData *)data appendNameString:(NSString *)name {
    NSString *cachesDirectory = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES).firstObject;
    NSString *createPath = [cachesDirectory stringByAppendingPathComponent:@"video"];
    NSFileManager *fileManager = [[NSFileManager alloc] init];
    [fileManager createDirectoryAtPath:createPath withIntermediateDirectories:YES attributes:nil error:nil];
    NSString *path = [cachesDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"/video/%.0f%@",[NSDate date].timeIntervalSince1970,name]];
    [data writeToFile:path atomically:NO];
    return path;
}



@end
